package com.youdianstar.app.ui.pay

import android.app.Dialog
import android.view.View
import com.youdianstar.app.R
import com.youdianstar.app.BR
import com.youdianstar.app.databinding.ActivityDetailBuyBinding
import com.youdianstar.app.common.Extra
import com.youdianstar.app.common.DataCode
import com.youdianstar.app.common.base.BaseActivity
import com.youdianstar.app.common.ex.getObject
import com.youdianstar.app.common.ex.setThrottleListener
import com.youdianstar.app.common.util.*
import com.youdianstar.app.entity.PaymentRecord
import com.youdianstar.app.util.DataAppManager
import com.youdianstar.app.util.DataHelperEx
import com.youdianstar.app.util.WalletDialogUtil
import com.youdianstar.app.common.network.BaseApiResultObserver
import org.koin.androidx.viewmodel.ext.android.viewModel


/**
 * File: BillDetailActivity
 * Author: 82149 Create: 2023/3/16 9:57
 * Changes (from 2023/3/16)
 * --------------------------------------------------
 * description: 流水详情: --购买详情改
 * ---------------------------------------------------
 *
 */
class BillDetailActivity :BaseActivity<ActivityDetailBuyBinding, WalletViewModel>(){
    override fun layoutId(): Int = R.layout.activity_detail_buy

    override fun initVariableId(): Int = BR.viewModel

    override fun initViewModel(): WalletViewModel {
        val model: WalletViewModel by viewModel()
        return model
    }

    private var transferNo :String = ""
    private var recordBean : PaymentRecord? =null
    private var receiptDialog: Dialog? = null

    override fun initViewLayout() {
        super.initViewLayout()
        mBinding.headBar.initTitle(this, getString(R.string.order_paymentDetail))
        transferNo = intent.getStringExtra(Extra.IT_ID)?:""
        recordBean = intent.getObject(Extra.IT_DATA)

        recordBean?.let {
            if (StrUtil.isEmpty(transferNo))
                transferNo = it.transferNo?:""
            setViewLayout(it)
        }
    }

    private fun setViewLayout(bean : PaymentRecord){
        LogUtil.i("---$bean")
        when(bean.type){
            DataCode.BisType_Rent->{
                mBinding.tvCategory.text = getString(R.string.wallet_rental)
                mBinding.tvPayStatus.text = getString(R.string.order_paid)
                mBinding.txRules.visibility = View.VISIBLE
                mBinding.llPlanContainer.visibility = View.VISIBLE
            }
            DataCode.BisType_Rent_Refund->{
                //不显示了(合并一条记录)
                mBinding.tvCategory.text = getString(R.string.wallet_rental)
                mBinding.tvPayStatus.text = getString(R.string.pay_refunded)
                mBinding.txRules.visibility = View.VISIBLE
                mBinding.llPlanContainer.visibility = View.VISIBLE
            }
            DataCode.BisType_Deposit->{
                mBinding.tvCategory.text = getString(R.string.wallet_deposit)
                mBinding.tvPayStatus.text = getString(R.string.order_paid)
            }
            DataCode.BisType_Deposit_Rf->{
                mBinding.tvCategory.text = getString(R.string.wallet_deposit)
                mBinding.tvPayStatus.text = getString(R.string.pay_refunded)
            }
            DataCode.BisType_Penalty->{
                mBinding.tvCategory.text = getString(R.string.pay_powerbank)
                mBinding.tvPayStatus.text = getString(R.string.order_paid)
                mBinding.txRules.visibility = View.VISIBLE
                mBinding.llPlanContainer.visibility = View.VISIBLE
            }
            DataCode.BisType_Penalty_Rf->{
                mBinding.tvCategory.text = getString(R.string.pay_powerbank)
                mBinding.tvPayStatus.text = getString(R.string.pay_refunded)
                mBinding.txRules.visibility = View.VISIBLE
                mBinding.llPlanContainer.visibility = View.VISIBLE
            }

            DataCode.BisType_Coupon,DataCode.BisType_Coupon_Rf->{
                mBinding.tvCategory.text = getString(R.string.pay_coupon)
                mBinding.tvPayStatus.text = getString(R.string.order_paid)
                mBinding.txRules.visibility = View.INVISIBLE
                mBinding.llPlanContainer.visibility = View.INVISIBLE
            }
            DataCode.BisType_Charging,DataCode.BisType_Charging_Rf->{
                mBinding.tvCategory.text = getString(R.string.charge_card)
                mBinding.tvPayStatus.text = getString(R.string.order_paid)
                mBinding.txRules.visibility = View.INVISIBLE
                mBinding.llPlanContainer.visibility = View.INVISIBLE
            }
        }
        var isRefund = false
        if (bean.paymentState>2 || bean.refund){
            isRefund = true
            if (bean.paymentState==3){
                mBinding.tvPayStatus.text = getString(R.string.payment_partialRefunded)
            }else{
                mBinding.tvPayStatus.text = getString(R.string.pay_refunded)
            }
        }
        /*订单编号 流水号*/
        mBinding.tvTransID.text = bean.transferNoTx()
        mBinding.tvOrderNo.text = bean.orderNoTx()
        //支付方式
        val methodTx = when(bean.paymentMethod){
            DataCode.Type_Card, DataCode.Type_NewCard->{
                if (StrUtil.isEmpty(bean.bankNo)){
                    getString(R.string.wallet_type_bankcard)
                }else{
                    getString(R.string.wallet_type_bankcard)+" ("+bean.bankNo+")"
                }
            }
            DataCode.Type_Google->{
                getString(R.string.wallet_type_googlepay)
            }
            DataCode.Type_Apple->{
                getString(R.string.wallet_type_applepay)
            }
            else -> {
                "--"
            }
        }
        mBinding.tvPaymentMethod.text = methodTx
        mBinding.tvPaymentDate.text = DateUtil.getOrderTime(bean.createTime)

        val symbolStr = DataAppManager.instance.symbolTx()
        //退款金额
        if (bean.type == DataCode.BisType_Rent || bean.type ==DataCode.BisType_Rent_Refund){
            mBinding.tvRefundAmount.text = "$symbolStr ${MathUtil.round(bean.refundAmount)}"
        }else{
            mBinding.tvRefundAmount.text = "$symbolStr ${MathUtil.round(bean.amount)}" //0509 不是租借业务就取支付金额
        }
        mBinding.clInfoRefund.visibility = if (isRefund) View.VISIBLE else View.GONE
        //税金
        if (bean.tax>0){
            mBinding.clInfoTax.visibility = View.VISIBLE
            mBinding.txTaxAmount.text = getString(R.string.pay_tax) + " "+MathUtil.round(bean.tax) + "%"
            mBinding.tvTaxAmount.text = symbolStr + MathUtil.round(bean.taxAmount)
            mBinding.txPaymentAmount.text = getString(R.string.order_paymentAmount) + " ("+getString(R.string.pay_includingTax)+")"
        }else{
            mBinding.clInfoTax.visibility = View.GONE
            mBinding.txPaymentAmount.text = getString(R.string.order_paymentAmount)
        }
        //实际支付金额
        mBinding.tvPayAmount.text = "$symbolStr ${MathUtil.round(bean.amount)}"

        //计费规则
        if (null!=bean.billing){
            mBinding.tvPlanContent.text = DataHelperEx.pricePlanTx(this,bean.billing)
        }else{
            mBinding.tvPlanContent.text = "--"
        }
        //已支付 未退款 且未开收据
        mBinding.llBottom.visibility = if (bean.sentReceipt || isRefund) View.INVISIBLE else View.VISIBLE
    }

    override fun initClickEvent() {
        mBinding.btnAction.setThrottleListener {
            showReceiptDialog()
        }

        mViewModel.receiptResultLiveData.observe(this,object : BaseApiResultObserver<Any>(true){
            override fun onDataResult(data: Any?) {
                super.onDataResult(data)
                dismissDialog()
                toastShow(getString(R.string.public_success))
                DataAppManager.instance.billUpdateTag = true
                mBinding.llBottom.visibility = View.INVISIBLE
            }

            override fun onNetError(code: Int, e: String?) {
                super.onNetError(code, e)
                dismissDialog()
                toastShow(e)
            }
        })
    }

    override fun onPause() {
        super.onPause()
        receiptDialog?.let {
            if (it.isShowing)
                it.dismiss()
        }
    }

    private fun showReceiptDialog() {
        recordBean?.let { result ->
            var emailStr: String? = null
            DataAppManager.instance.getUser()?.let {
                emailStr = it.receiptEmail
            }
            receiptDialog = WalletDialogUtil.instance.showReceiptDialog(this, emailStr ?: "") { email ->
                    mViewModel.sendPaymentReceipt(billID = result.id, orderNo = result.orderNo ?: "", email = email)
                }
        }
    }
}